package com.project.smarthome;

import android.app.Activity;
import android.os.Build;
import android.support.multidex.MultiDexApplication;

import com.project.smarthome.base.ActivityObserver;
import com.project.smarthome.base.ApplicationPresenter;
import com.project.smarthome.common.util.SystemUtils;
import com.sds.sdk.android.sh.SHLog;
import com.sds.sdk.android.sh.common.SHDebug;
import com.sds.smarthome.foundation.DataSourceFactory;
import com.sds.smarthome.foundation.debug.AppConfig;
import com.sds.smarthome.foundation.util.XLog;
import com.squareup.leakcanary.LeakCanary;

import java.io.File;
import java.util.ArrayList;
import java.util.List;


public class SmartApplication extends MultiDexApplication {

    private static SmartApplication sContext;
    public static boolean sIsnormalstart;
    private ApplicationPresenter mApppresenter;
    private List<Activity> mActivitylist;

    @Override
    public void onCreate() {
        super.onCreate();
        if (!SystemUtils.isMainProcess(this)) {
            return;
        }
        if (BuildConfig.DEBUG) {
            SHDebug.setCcuName("CCU_00018");
        }
        sContext = this;
        DataSourceFactory.getAppCache().initialize(this);

        XLog.init(this, BuildConfig.DEBUG, true, "smartHome" + File.separator + "log");
        SHLog.setLogCallback(new SHLog.LogCallback() {
            @Override
            public void onVerbose(String s) {
                XLog.f(s);
            }

            @Override
            public void onDebug(String s) {
                XLog.f(s);
            }

            @Override
            public void onInfo(String s) {
                XLog.f(s);
            }

            @Override
            public void onWarn(String s) {
                XLog.f(s);
            }

            @Override
            public void onError(String s) {
                XLog.f(s);
            }
        });
        mActivitylist = new ArrayList<>();
        ActivityObserver activityObserver = new ActivityObserver();
        registerActivityLifecycleCallbacks(activityObserver);

        AppConfig.getAppConfig().loadConfig();
        SHLog.enable(AppConfig.getAppConfig().isEnableSdkLog());
        SHLog.setEnableRecvMsg(AppConfig.getAppConfig().isEnableSdkLog());
        XLog.setEnableUpload(AppConfig.getAppConfig().isEnableLogUpload());
        SHDebug.setCcuName("CCU_" + AppConfig.getAppConfig().getDebugCcuId());
        SHDebug.setUseLocalServer(AppConfig.getAppConfig().isUseLocalTranServer());
        SHDebug.setUseSsl(AppConfig.getAppConfig().isEnableSsl());
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            SHDebug.setUseSsl(false);
        }

        mApppresenter = ApplicationPresenter.initializeWithApp(this, activityObserver);
        mApppresenter.init();
        LeakCanary.install(this);
    }



    public static SmartApplication getContext() {
        return sContext;
    }

    public void addActivity(Activity activity) {
        mActivitylist.add(activity);
    }

    public void removeActivity(Activity activity) {
        mActivitylist.remove(activity);
    }

    public List<Activity> getActivitylist() {
        return mActivitylist;
    }

    public void exitApp() {
        for (Activity activity : mActivitylist) {
            activity.finish();
        }
        mActivitylist.clear();
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        mApppresenter.destroy();
    }
}
